﻿namespace CompanyPayRollSystem
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using CompanyPayRollSystem.BusinessLayer;
    using CompanyPayRollSystem.Database;

    /// <summary>
    /// Add Employee form
    /// </summary>
    public partial class AddEmployeeForm : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.Windows.Forms.Form"/> class.
        /// </summary>
        public AddEmployeeForm()
        {
            InitializeComponent();
            this.LoadDepartments();
        }

        /// <summary>
        /// Handles the Click event of the btnAdd control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            int departmentID, jobLevelID;
            double salary;
            if (this.cmbDepartments.SelectedIndex > -1)
            {
                departmentID = ((Department)this.cmbDepartments.SelectedItem).DepartmentID;
            }
            else
            {
                MessageBox.Show("Specify department.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!double.TryParse(this.txtSalary.Text, out salary))
            {
                MessageBox.Show("Invalid Salary format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                EmployeeManager.AddEmployee(this.txtName.Text, departmentID, this.txtTitle.Text, salary);
                MessageBox.Show("Employee added successfully.", "Added", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Employee not added.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region Load Comboboxes

        /// <summary>
        /// Loads the departments.
        /// </summary>
        private void LoadDepartments()
        {
            List<Department> departments = DepartmentManager.GetDepartments();
            this.cmbDepartments.DisplayMember = ColumnsNames.DepartmentColumnsNames.DepartmentName;
            this.cmbDepartments.ValueMember = ColumnsNames.DepartmentColumnsNames.DepartmentID;
            this.cmbDepartments.DataSource = departments;
        }

        #endregion
    }
}
